package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Craft;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface CraftMapper {
    @Insert("INSERT INTO `craft`" +
            " (`line_id`,`name`,`tenant_id`) VALUES" +
            " (#{lineId}, #{name},#{tenantId})")
    void create(Craft craft);

    @Select("<script>" +
            "SELECT * FROM `craft` " +
            "<where>" +
            "<if test='craft.lineId!=null'> AND `line_id`=#{craft.lineId}</if>" +
            "<if test='craft.name!=null'> AND `name` LIKE CONCAT('%',#{craft.name},'%') </if>" +
            " AND `status`!=" + User.STATUS_DELETE +
            " AND `tenant_id`=#{craft.tenantId} " +
            "</where>"+
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Craft> findAll(@Param("craft") Craft craft, @Param("pagination") Pagination pagination);


    @Select("SELECT * FROM `craft` WHERE  `id` = #{id}  AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Craft findCraftById(@Param("tenantId") Long tenantId, @Param("id") Long id);


    @Update("UPDATE `craft` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId}  AND `status`!=" + User.STATUS_DELETE)
    int delete(@Param("tenantId") Long tenantId, @Param("id") Long id);

    @Update("UPDATE `craft` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `line_id`=#{LineId} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteByLineId(@Param("tenantId") Long tenantId, @Param("LineId") Long LineId);

    @Update("UPDATE `craft` SET `name` = #{name} , `line_id` = #{lineId}" +
            " WHERE `id` = #{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int update(Craft craft);



    @Select("<script>" +
            "SELECT count(*) FROM `craft` " +
            "<where>" +
            "<if test='craft.lineId!=null'> AND `line_id`=#{craft.lineId}</if>" +
            "<if test='craft.name!=null'> AND `name` LIKE CONCAT('%',#{craft.name},'%') </if>" +
            " AND `tenant_id`=#{craft.tenantId} AND `status`!=" + User.STATUS_DELETE +
            "</where>"+
            "</script>")
    long count(@Param("craft") Craft craft);

    @Select("SELECT * FROM `craft` WHERE `name` = #{craft.name} and `line_id`= #{craft.lineId} AND `tenant_id`=#{craft.tenantId}  AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    List<Craft> findCraftByName(@Param("craft") Craft craft);
}
